package us.curino.sql2cat.server;

import java.util.StringTokenizer;
import java.util.Vector;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import us.curino.sql2cat.client.CommuteContainer;
import us.curino.sql2cat.client.generateCategoryGraph;
import com.relationalcloud.SQL2Category;
import com.relationalcloud.tsqlparser.Parser;
import com.relationalcloud.tsqlparser.exception.NotImplementedException;
import com.relationalcloud.tsqlparser.schema.*;
import com.relationalcloud.tsqlparser.loader.IntegrityConstraintsExistsException;
import com.relationalcloud.tsqlparser.loader.PrimaryKey;
import com.relationalcloud.tsqlparser.loader.Schema;
import com.relationalcloud.tsqlparser.parser.ParseException;
import com.relationalcloud.tsqlparser.schema.Index;
import com.relationalcloud.tsqlparser.loader.SchemaTable;
import com.relationalcloud.tsqlparser.statement.create.table.ColumnDefinition;

public class generateCategoryGraphImpl extends RemoteServiceServlet implements generateCategoryGraph {

	SQL2Category s2;
	
	public generateCategoryGraphImpl(){
		
		try {
			s2= new SQL2Category();
		} catch (IntegrityConstraintsExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public CommuteContainer sendSQL(String sql) {
		
		System.out.println(sql);

		try {
			Parser p =new Parser("tpcc",s2.schema,sql);
			CommuteContainer c = new CommuteContainer();
			
			c.schema = SQL2Category.generateSchemaGraph(s2);
			c.sel = SQL2Category.generateQuerySelectionGraph(p, s2.schema);
			c.proj = "";
			c.functPull = SQL2Category.generateQueryProjectGraph(s2, p);

			return c;
			
		} catch (NotImplementedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public CommuteContainer sendSchemaSQL(String text) {
		
		StringTokenizer st = new StringTokenizer(text,";");
		
		
		s2.schema = new Schema();
		
		while(st.hasMoreTokens()){
			Parser p;
			try {
				String s = st.nextToken();
				if(s.length()>2){
				p = new Parser("tpcc",s2.schema,s);
				
				SchemaTable t = new SchemaTable(p.getTableList().get(0).getName());
			for(ColumnDefinition cd:p.getCreateTableColumnDefinitions()){
				t.addColumn(cd.getColumnName().getColumnName(),cd.getColDataType().getDataTypeName());
			}
			
			int k=1;
			if(p.getCreateTableConstraints()!=null)
			for(Index i:p.getCreateTableConstraints()){
				
				if(i instanceof ForeignKey){
					try {
						Vector<String> source = new Vector<String>(((ForeignKey)i).getColumnsNames());
						Vector<String> target = new Vector<String>(((ForeignKey)i).getReferenceDefinition().getColumnsReferenced());
						String fkid =((ForeignKey)i).getName();
						String targettable = ((ForeignKey)i).getReferenceDefinition().getTableReferenced().getName();
						com.relationalcloud.tsqlparser.loader.ForeignKey f = new com.relationalcloud.tsqlparser.loader.ForeignKey(fkid, source, targettable,target);
						t.addConstraint(f);
					} catch (IntegrityConstraintsExistsException e1) {
						e1.printStackTrace();
					}
				}
				else{
						Vector<String> v = new Vector<String>();
						v.addAll(i.getColumnsNames());
						try {
							t.addConstraint(new PrimaryKey("pk"+(k++),v));
						} catch (IntegrityConstraintsExistsException e) {
							e.printStackTrace();
						}
				}
			}
			s2.schema.addTable(t);
			
				
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		
		
		CommuteContainer c = new CommuteContainer();
		c.schema = SQL2Category.generateSchemaGraph(s2);
		
		return c;
		
		
	}


	
}
